<?php
class ModelSettingTemplate extends Model {
	public function addTemplate($data) {
		$this->db->query("INSERT INTO " . DB_PREFIX . "template SET name = '" . $this->db->escape($data['name']) . "', directory = '" . $this->db->escape($data['directory']) . "', type = '" . $this->db->escape($data['type']) . "', status = '" . (int)$data['status'] . "', date_added = NOW()");
		
		$template_id = $this->db->getLastId();
		
		if (isset($data['restaurants'])) {
			foreach ($data['restaurants'] as $restaurant) {
				$this->db->query("INSERT INTO " . DB_PREFIX . "template_to_restaurant SET template_id = '" . (int)$template_id . "', restaurant_id = '" . (int)$restaurant['restaurant_id'] . "'");
			}
		}
	}
	
	public function editTemplate($template_id, $data) {
		$this->db->query("UPDATE " . DB_PREFIX . "template SET name = '" . $this->db->escape($data['name']) . "', directory = '" . $this->db->escape($data['directory']) . "', type = '" . $this->db->escape($data['type']) . "', status = '" . $this->db->escape($data['status']) . "', date_modified = NOW() WHERE template_id = '" . (int)$template_id . "'");

		$this->db->query("DELETE FROM " . DB_PREFIX . "template_to_restaurant WHERE template_id = '" . (int)$template_id . "'");

		if (isset($data['restaurants'])) {
			foreach ($data['restaurants'] as $restaurant) {
				$this->db->query("INSERT INTO " . DB_PREFIX . "template_to_restaurant SET template_id = '" . (int)$template_id . "', restaurant_id = '" . (int)$restaurant['restaurant_id'] . "'");
			}
		}
	}
	
	public function deleteTemplate($template_id) {
		$this->db->query("DELETE FROM " . DB_PREFIX . "template WHERE template_id = '" . (int)$template_id . "'");
	}
	
	public function getTemplate($template_id) {
		$sql = "SELECT * FROM " . DB_PREFIX . "template WHERE template_id = '" . $template_id . "'";
		
		$query = $this->db->query($sql);
				
		return $query->row;
	}
	
	public function getTemplates($data = array()) {
		if ($data) {
			$sql  = "SELECT DISTINCT t.*, ";
			$sql .= "(";
			$sql .= " 	SELECT COUNT(setting_id) AS total FROM " . DB_PREFIX . "setting";
			$sql .= " 		WHERE `group` = 'config'";
			$sql .= " 			AND `key` = 'config_template'";
			$sql .= " 			AND `value` = t.template_id";
			$sql .= ") > 0 AS 'inUse'";
			$sql .= " FROM " . DB_PREFIX . "template t";
				
			if (!empty($data['filter_restaurant_name']) || !empty($data['filter_restaurant_id'])) {
				$sql .= " INNER JOIN " . DB_PREFIX . "template_to_restaurant tts ON t.template_id = tts.template_id";
				$sql .= " INNER JOIN " . DB_PREFIX . "restaurant s ON tts.restaurant_id = s.restaurant_id";
			}
			
			$sql .= " WHERE 1 = 1";
			 			
			if (!empty($data['filter_name'])) {
				$sql .= " AND LCASE(t.name) LIKE '" . $this->db->escape(utf8_strtolower($data['filter_name'])) . "%'";
			}
	
			if (isset($data['filter_type']) && !is_null($data['filter_type'])) {
				$sql .= " AND t.type = '" . (int)$data['filter_type'] . "'";
			}
			
			if (!empty($data['filter_restaurant_name'])) {
				$sql .= " AND LCASE(s.name) LIKE '" . $this->db->escape(utf8_strtolower($data['filter_restaurant_name'])) . "%'";
			}
		
			if (!empty($data['filter_restaurant_id'])) {
				$sql .= " AND LCASE(s.restaurant_id) LIKE '" . (int)$data['filter_restaurant_id'] . "%'";
			}
			
			if (isset($data['filter_status']) && !is_null($data['filter_status'])) {
				$sql .= " AND t.status = '" . (int)$data['filter_status'] . "'";
			}
			
			$sql .= " GROUP BY t.template_id";
						
			$sort_data = array(
				't.name',
				't.type',
				't.status'
			);	
			
			if (isset($data['sort']) && in_array($data['sort'], $sort_data)) {
				$sql .= " ORDER BY " . $data['sort'];	
			} else {
				$sql .= " ORDER BY t.name";	
			}
			
			if (isset($data['order']) && ($data['order'] == 'DESC')) {
				$sql .= " DESC";
			} else {
				$sql .= " ASC";
			}
		
			if (isset($data['start']) || isset($data['limit'])) {
				if ($data['start'] < 0) {
					$data['start'] = 0;
				}				

				if ($data['limit'] < 1) {
					$data['limit'] = 20;
				}	
			
				$sql .= " LIMIT " . (int)$data['start'] . "," . (int)$data['limit'];
			}	
			
			$query = $this->db->query($sql);
		
			return $query->rows;
		} else {
			$sql = "SELECT DISTINCT t.* FROM " . DB_PREFIX . "template t";
			
			$sql .= " INNER JOIN " . DB_PREFIX . "template_to_restaurant tts ON t.template_id = tts.template_id";
			$sql .= " ORDER BY t.name ASC";
			
			$query = $this->db->query($sql);

			$template_data = $query->rows;
	
			return $template_data;
		}
	}
	
	public function getTotalTemplates($data = array()) {
		$sql = "SELECT COUNT(DISTINCT t.template_id) AS total FROM " . DB_PREFIX . "template t";
		
		if (!empty($data['filter_restaurant_name'])) {
			$sql .= " INNER JOIN " . DB_PREFIX . "template_to_restaurant tts ON t.template_id = tts.template_id";
			$sql .= " INNER JOIN " . DB_PREFIX . "restaurant s ON tts.restaurant_id = s.restaurant_id";
		}
		
		$sql .= " WHERE 1 = 1";
		 			
		if (!empty($data['filter_name'])) {
			$sql .= " AND LCASE(t.name) LIKE '" . $this->db->escape(utf8_strtolower($data['filter_name'])) . "%'";
		}

		if (isset($data['filter_type']) && !is_null($data['filter_type'])) {
			$sql .= " AND t.type = '" . (int)$data['filter_type'] . "'";
		}
		
		if (!empty($data['filter_restaurant_name'])) {
			$sql .= " AND LCASE(s.name) LIKE '" . $this->db->escape(utf8_strtolower($data['filter_restaurant_name'])) . "%'";
		}
		
		if (isset($data['filter_status']) && !is_null($data['filter_status'])) {
			$sql .= " AND t.status = '" . (int)$data['filter_status'] . "'";
		}

		$query = $this->db->query($sql);
		
		return $query->row['total'];
	}
}
?>